package cn.abcsys.cloud.devops.web.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.abcsys.cloud.devops.web.dao.RoleActionMapper;
import cn.abcsys.cloud.devops.web.dao.RoleMapper;
import cn.abcsys.cloud.devops.web.dao.UserRoleEnvProjMapper;
import cn.abcsys.cloud.devops.web.entity.GridBean;
import cn.abcsys.cloud.devops.web.entity.PagerModel;
import cn.abcsys.cloud.devops.web.entity.Role;
import cn.abcsys.cloud.devops.web.entity.RoleAction;
import cn.abcsys.cloud.devops.web.entity.User;
import cn.abcsys.cloud.devops.web.entity.UserRoleEnvProj;
import cn.abcsys.cloud.devops.web.service.RoleService;

/**
 * 角色相关服务实现类
 * @author xzg
 *
 */
@Service
public class RoleServiceImp implements RoleService{

	private static final Logger LOGGER = Logger.getLogger(RoleServiceImp.class);
	@Resource
	private RoleMapper roleMapper;
	
	@Resource
	private UserRoleEnvProjMapper userRoleMapper;
	
	@Resource
	private RoleActionMapper roleActionMapper;
	
/*	@Resource
	private UserProjectMapper userProjectMapper;*/
	@Override
	public List<Role> getRolePermmitMod(User user) {
		// TODO Auto-generated method stub
		return roleMapper.getRolePermmitMod(user);
	}
	/**
	 * 增加角色
	 * @throws SQLException 
	 */
	@Override
	public int addRole(Role role) {
		// TODO Auto-generated method stub
			int re = 0;
				try {
					//默认新增角色状态正常
					role.setRoleStatus((byte)1);
					re = roleMapper.insertRole(role);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return re;
	}
	
	/** 
	 * 批量增加用户角色
	 * 
	 */
	@Transactional
	@Override
	public int addUserRole(List<UserRoleEnvProj> urej) {
		// TODO Auto-generated method stub
		try {
		//1、批量增加用户角色时，这里注意的地方时，在用户和环境确定的情况下不允许有重复的角色，所有线删除该用户在本环境下的所有角色
			UserRoleEnvProj userRoleEnv = urej.get(0);
			userRoleMapper.deleteByUserIdEnvId(userRoleEnv);
		//2、批量增加
			userRoleMapper.insertListUREJ(urej);
			return 	1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return 0;
		}
		
	}
	
	/**
	 * 批量增加角色权限
	 * @see com.oncecloud.devops.client.service.RoleService#addRoleAuthority(java.util.List)
	 */
	@Override
	public int addRoleAuthority(List<RoleAction> roleActions) {
		// TODO Auto-generated method stub
		 try {
			return roleActionMapper.insertListRA(roleActions);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 return 0;
		}
		
	}
	
	@Override
	public int updateRoleById(Role role) throws SQLException {
		// TODO Auto-generated method stub
		return roleMapper.updateRoleById(role);
	}
	
	/**
	 * @see com.oncecloud.devops.client.service.RoleService#deleteRoleAction(java.util.List)
	 */
	@Override
	public int deleteRoleAction(List<Integer> list) throws SQLException {
		// TODO Auto-generated method stub
		 try {
			 return roleActionMapper.deleteRoleActions(list);
			 
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				 return 0;
			}
	}
	
	@Override
	public GridBean getRoleByName(Role role,PagerModel pagerModel) {
		int page = pagerModel.getPage();
		int rows = pagerModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<Role> pageObject = new Page<Role>();
		Map<String,Object> map = new HashMap<>();
		map.put("roleName", role.getRoleName());
		map.put("roleDesc", role.getRoleDesc());
		map.put("roleStatus", role.getRoleStatus());
		map.put("roleRemark", role.getRoleRemarks());
		PageHelper.startPage(page, rows);
		List<Role> roleList = roleMapper.getRoles(map);
		for(Role rol: roleList){
			pageObject.add(rol);
		}
		totalpage = ((Page<?>) roleList).getPages();
		totalNum = ((Page<?>) roleList).getTotal();
		GridBean gridBean = new GridBean(page, totalpage, totalNum.intValue(), pageObject);
		return gridBean;
	}
	@Override
	public int deleteRoleById(Integer roleId) {
		
		try {
			
			return roleMapper.deleteRoleById(roleId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	@Override
	public int addRoleUserEnv(UserRoleEnvProj ure) {
		// TODO Auto-generated method stub
		try {
			return userRoleMapper.insert(ure);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("为用户添加角色信息失败");
			return 0;
		}
	}
	@Transactional
	@Override
	public int addRoleActions(List<RoleAction> roleActionList) {
		// TODO Auto-generated method stub
		try {
			return roleActionMapper.insertListRA(roleActionList);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOGGER.error("批量插入失败");
			return 0;
		}
	}
	@Override
	public List<Role> getRoleByUserEnv(UserRoleEnvProj ure) {
		// TODO Auto-generated method stub
		return roleMapper.getUserEnvRole(ure);
	}
	
/*	@Transactional
	@Override
	public void deleteUserEnvRoleProj(Integer userId, Integer envId) throws SQLException {
		// TODO Auto-generated method stub
		//删除与用户相关的环境、角色、项目信息
		userRoleMapper.deleteByUserIdenvId(userId,envId);
		userProjectMapper.deleteProjectUserByUserId(userId,envId);
	}*/
	
}
